#!/bin/sh

. /usr/share/common

USB_SCRIPT="/etc/init.d/F02failsafe-usb"

start() {
	if [ "true" = "$usb_console" ] && [ -f "$USB_SCRIPT" ]; then
		exit 0
	fi

	# Always enter failsafe if enable_failsafe is true
	if [ "true" = "$enable_failsafe" ]; then
		echo_c 118 "Automatically entering failsafe mode..."
		touch /tmp/.fs
		. /etc/profile
		exec setsid /sbin/getty -n -l /bin/sh -L console 0 vt100
		return
	fi

	echo_c 104 "\nPress the [f] key to enter failsafe mode\n"

	stty -icanon min 0 time 20 -echo
	key=$(dd bs=1 count=1 2>/dev/null)
	stty sane

	if [ "f" = "$key" ]; then
		touch /tmp/.fs
		. /etc/profile
		exec setsid /sbin/getty -n -l /bin/sh -L console 0 vt100
		echo_c 118 "Failsafe mode..."
	fi

	if [ -f "$OS_RELEASE_FILE" ]; then
		. "$OS_RELEASE_FILE"
		echo_c 208 "Thingino firmware built for $IMAGE_ID\n[$BUILD_ID]\n"
	fi

	/etc/init.d/rcS
}

case "$1" in
	start)
		start
		;;
	stop)
		true
		;;
	restart)
		true
		;;
	*)
		true
		;;
esac

exit 0
